package com.henu.xyh;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.henu.R;
import com.henu.adapter.ListReplyAdapter;
import com.henu.xyh.entity.Express;
import com.henu.xyh.entity.Reply;
import com.henu.yizhiya.entity.Person;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

public class Express_ReplyActivity extends AppCompatActivity {

    private TextView express_user;
    private TextView express_time;
    private TextView express_content;
    private ListView reply_listview;
    ArrayList<Reply> replylist=new ArrayList<>();
    ListReplyAdapter adapter;
    private EditText edit_reply;
    private ImageButton btn_reply;
    private ImageButton btn_return;
    private String e_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_express__reply);

        Intent intent = getIntent();
        Express express = (Express) intent.getSerializableExtra("listview");
        e_id = express.getObjectId();//得到该表白的Id，然后查询此id下的回复信息。
        //显示表白内容
        express_user = (TextView) findViewById(R.id.express_reply_user);
        express_user.setText(express.getUser_Id().getUsername());
        express_content = (TextView) findViewById(R.id.express_reply_content);
        express_content.setText(express.getExpress_Message());
        express_time = (TextView) findViewById(R.id.express_reply_time);
        express_time.setText(express.getCreatedAt());
        //显示回复信息
        reply_listview = (ListView) findViewById(R.id.reply_listview);
        adapter = new ListReplyAdapter(this);
        reply_listview.setAdapter(adapter);
        Bmob.initialize(this, "a24db0fac16b375a97072b85080ea20f");
        //写入类别数据
        final BmobQuery<Reply> query = new BmobQuery<Reply>();
        query.include("User_Id");
        query.addWhereEqualTo("Express_Id",e_id);
        query.findObjects(new FindListener<Reply>() {
            @Override
            public void done(List<Reply> list, BmobException e) {
                if(e==null){
                    replylist=(ArrayList)list;
                    adapter.addData(replylist);
                    adapter.notifyDataSetChanged();
                }
                else {
                    System.out.println("bmob defect!"+e.getMessage()+","+e.getErrorCode());
                }
            }
        });
        //回复信息设置
        edit_reply= (EditText) findViewById(R.id.edit_reply);
        btn_reply= (ImageButton) findViewById(R.id.btn_reply);
        btn_reply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String reply_content=edit_reply.getText().toString().trim();
                if(reply_content==null||reply_content.equals(""))
                {
                    Toast.makeText(Express_ReplyActivity.this, "请输入内容！", Toast.LENGTH_SHORT).show();
                }
                else {
                    final Reply reply=new Reply();
                    Person person = BmobUser.getCurrentUser(Person.class);
                    reply.setUser_Id(person);
                    reply.setReply_Message(reply_content);
                    Express ex = new Express();
                    ex.getObjectId();
                    ex.setObjectId(e_id);
                    reply.setExpress_Id(ex);

                    reply.save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                            if (e==null)
                            {
                                Toast.makeText(Express_ReplyActivity.this, "评论成功。", Toast.LENGTH_SHORT).show();
                                BmobQuery<Reply> query = new BmobQuery<Reply>();
                                query.include("User_Id");
                                query.order("-createdAt");
                                query.addWhereEqualTo("Express_Id",e_id);
                                query.findObjects(new FindListener<Reply>() {
                                    @Override
                                    public void done(List<Reply> list, BmobException e) {
                                        if(e==null){
                                            replylist=(ArrayList)list;
                                            adapter.addData(replylist);
                                            adapter.notifyDataSetChanged();
                                            edit_reply.setText("");
                                        }
                                        else {
                                            System.out.println("bmob defect!"+e.getMessage()+","+e.getErrorCode());
                                        }
                                    }
                                });
                            }
                            else {
                                Toast.makeText(Express_ReplyActivity.this, "评论失败！", Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }

            }
        });

        btn_return= (ImageButton) findViewById(R.id.reply_return);
        btn_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}
